﻿@{
    CurrentUser user = ViewBag.CurrentUser;
    IEnumerable<dynamic> fieldData = Html.GetFieldList((string)ViewBag.Table, "list", user);
}
<div class="current-location">
    <ul>
        <li class="left"><i class="glyphicon glyphicon-home"></i><a href="javascript:void(0)" onclick="location.href=location.href">部门管理</a></li>
        <li class="right"><a href="javascript:void(0)" onclick="LoadData()" title="刷新"><span class="glyphicon glyphicon-refresh"></span></a></li>
    </ul>
</div>
<div class="main">
    <table class="tb-head" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td class="btn-group">
                <button type="button" class="btn btn-default btn-sm ui-selectInvert">反选</button>
                <button type="button" class="btn btn-default dropdown-toggle btn-sm" data-toggle="dropdown" aria-expanded="false">
                    菜单
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu" role="menu">
                    <li><a class="ui-dialog" data-custom-params="title:'添加部门', url:'@Url.ActionUrl("Add")',width:'90%',height:'95%'" href="javascript:void(0)">添加</a></li>
                    <li class="divider"></li>

                    <li><a href="javascript:void(0)" class="ui-setField" data-custom-params="url:'@Url.ActionUrl("SetField")',setField:'isFinal',setValue:'1',limitFinal:'0',includeChildNode:'0',callBack:'LoadData'">转为最终部门</a></li>
                    <li><a href="javascript:void(0)" class="ui-setField" data-custom-params="url:'@Url.ActionUrl("SetField")',setField:'isFinal',setValue:'0',limitFinal:'1',callBack:'LoadData'">转为非最终部门</a></li>
                    <li class="divider"></li>
                    <li><a href="javascript:void(0)" class="ui-setMultiple" data-custom-params="url:'@Url.ActionUrl("Transfer")',width:'700px',height:'80%'">转移</a></li>
                    <li class="divider"></li>
                    <li><a href="javascript:void(0)" class="ui-deleteMultiple" data-custom-params="url:'@Url.ActionUrl("Delete")',callBack:'LoadData',reverseExecute:true">删除</a></li>
                </ul>
            </td>
        </tr>
    </table>
    <table border=0 cellpadding=0 cellspacing=0 class="table table-bordered" id="tb_list">
        <thead>
            <tr>
                <td width="30px"><input type="checkbox" title="选择" class="ui-selectInvert" data-custom-params="isReverse:false"></td>
                <td align="left"><span>名称/</span><span class="ui-folder-switch">展开</span></td>
                <td align="center">Id</td>
                @{
                    foreach (var item in fieldData)
                    {
                        <td align="center">@item.Remark</td>
                    }
                }
                <td align="center">序号</td>
                <td align="center">添加下级</td>
                <td align="center">菜单权限</td>
                <td align="center">投稿权限</td>
                <td align="center">操作</td>
            </tr>
        </thead>
        <tr class="item list-template-item" id='item_{id}' data-id="{id}" data-parentId="{parentId}" data-isFinal="{isFinal}">
            <td align="center" width="30px"><input type="checkbox" class="checked-item" value="{id}"></td>
            <td class="form-inline">{space}<span class="node node_{isFinal}"></span><span>{name}</span></td>
            <td align="center">{id}</td>
            @foreach (var item in fieldData)
            {
                int listWords = item.ListWords;
                int fieldId = item.Id;
                string field = item.Name;
                string fieldType = item.FieldType;
                string valueType = item.ValueType;
                string align = "center";
                if (valueType == "nvarchar" || valueType == "text")
                {
                    if (fieldType != "image")
                    {
                        align = "left";
                    }
                }
                <td align="@align">
                    @if (new string[] { "images", "files", "subtable" }.Contains(fieldType))
                    {
                        <span class="sp_@field sp_values" data-value-params="id:{id},fieldId:@fieldId,name:'@field',fieldType:'@fieldType',valueType:'@valueType',listWords:'@listWords'"><i class="fa fa-spinner fa-spin"></i></span>
                    }
                    else
                    {
                        <span class="sp_@field sp_values" data-value-params="id:{id},fieldId:@fieldId,name:'@field',fieldType:'@fieldType',valueType:'@valueType',listWords:'@listWords'">{@field}</span>
                    }
                </td>
            }

            <td align="center" class="form-inline form-group-sm"><input type="text" name="xuhao" id="xuhao" data-table="pa_department" data-id="{id}" class="form-control ui-listXuhao" value='{xuhao}' style="width:50px" maxlength="3" /></td>
            <td align="center" class="form-inline">
                <button type="button" data-custom-params="title:'{name}-添加子部门',url:'@Url.ActionUrl("add")?parentId={id}',width:'90%',height:'95%'" class="btn btn-default btn-sm add_child_{isFinal} ui-dialog">添加</button>
            </td>
            <td align="center">
                <button class="btn btn-default btn-sm ui-dialog btn-{isFinal}" data-custom-params="title:'{name}-菜单权限设置',url:'@Url.ActionUrl("Index","DepartmentPermissions")?departmentId={id}',width:'90%',height:'95%'">设置</button>
            </td>
            <td align="center"><a href="javascript:void(0)" class="btn btn-default btn-sm ui-dialog btn-{isFinal}" data-custom-params="title:'{name}-投稿限设置',url:'@Url.ActionUrl("Index","DepartmentInfoPermissions")?departmentId={id}',width:'90%',height:'95%'">设置</a></td>
            <td align="center">
                <input type="hidden" name="id" value="{id}" />
                <div class="btn-group btn-group-sm">
                    <button type="button" data-custom-params="title:'{name}-修改',url:'@Url.ActionUrl("Edit")?id={id}',width:'90%',height:'95%'" class="btn btn-default btn-sm ui-dialog">修改</button>
                    <button type="button" data-custom-params="url:'@Url.ActionUrl("Delete")',callBack:'LoadData'" class="btn btn-default btn-sm ui-deleteSingle">删除</button>
                </div>
            </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    function LoadData() {
        var url = "@Url.ActionUrl("ListData")";
        $("#tb_list").TreeBind({ url: url, callBack: "LoadedInit" });

    }
    function LoadedInit() {
        $(".add_child_1").attr("disabled", "disabled");
        $(".btn-0").attr("disabled", "disabled");
        var $sp_values = $(".sp_values");
        $sp_values.each(function () {
            var $this = $(this);
            var thisHtml = $this.html();
            var params = $this.attr("data-value-params")
            params = ExtendObject(params);
            var parentId = params.id;
            var listWords = parseInt(params.listWords);
            var valueType = params.valueType;
            var fieldType = params.fieldType;
            var field = params.name;
            var fieldId = params.fieldId;
            if (fieldType == "subtable") {
                $.get("/E/SubData/Count", { _fieldId: fieldId, parentId: parentId }, function (data) { $this.text(data) });
            }
            else if (fieldType == "images" || fieldType == "files") {
                $.get("/E/AttachmentsData/Count", { _fieldId: fieldId, parentId: parentId }, function (data) { $this.text(data) });
            }
            else if (params.fieldType == "image" && thisHtml != "") {
                $this.html("<a href=\"" + thisHtml + "\" target=\"_blank\"><img src=\"" + thisHtml + "\" class=\"ui-thumbnail thumbnail\"></a>");
            }
            else if (valueType == "nvarchar" || valueType == "ntext" || valueType == "datetime") {
                if (thisHtml.length > listWords) {
                    if (field == "title") {
                        $this.html(thisHtml.Left(listWords) + "...");
                    }
                    else {
                        $this.html(thisHtml.Left(listWords));
                    }
                }
            }
        });
    }
    LoadData();

</script>